package com.htandroid.htandroid.adapter;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.htandroid.htandroid.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class MyAdapter_jtgl_jtsplist extends BaseAdapter {
    ArrayList<Map<String, Object>> data;
    Context context;

    private HashMap<Integer, Boolean> isSelected;

    public MyAdapter_jtgl_jtsplist(Context context, ArrayList<Map<String, Object>> data) {
        this.data = data;
        this.context = context;
        isSelected = new HashMap<Integer, Boolean>();
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return (Integer.valueOf(data.get(position).get("index").toString()) - 1);
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(this.context).inflate(R.layout.basenvo_jtgl_jtsplist, parent, false);

            holder.background = (ImageView) convertView.findViewById(R.id.item_iv);
            holder.index = (TextView) convertView.findViewById(R.id.item_index);
            holder.tv1 = (TextView) convertView.findViewById(R.id.cvouchid_tv);
            holder.tv2 = (TextView) convertView.findViewById(R.id.cdep_tv);
            holder.tv3 = (TextView) convertView.findViewById(R.id.cnocheckreason_tv);
            holder.tv4 = (TextView) convertView.findViewById(R.id.ddate_tv);
            holder.tv5 = (TextView) convertView.findViewById(R.id.czzabbre_tv);
            holder.tv6 = (TextView) convertView.findViewById(R.id.sum_tv);
            holder.more = (ImageView) convertView.findViewById(R.id.item_btn_more);
            holder.more.setTag(position);
            //convertView.setTag(convertView.hashCode(),position);
            //Log.i("", position+"|convertView="+convertView.hashCode());
            holder.more.setOnClickListener(new MyOnClickListener(holder.tv1) {
                @Override
                public void onClick(View v) {
                    final ImageView img = (ImageView) v;
                    //弹出窗口后,变更更多图标
                    img.setImageResource(R.drawable.item_more_choose);
                    //
                    View dialog = LayoutInflater.from(context).inflate(R.layout.basenvo_item_moreview, null, false);
                    final PopupWindow popupWindow = new PopupWindow(dialog, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
                    popupWindow.setTouchable(true);
                    popupWindow.setOutsideTouchable(true);
                    popupWindow.setBackgroundDrawable(new BitmapDrawable());
                    popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                        public void onDismiss() {
                            //还原图标
                            img.setImageResource(R.drawable.item_more);
                        }
                    });
                    //more
                    TextView more_tv1 = (TextView) dialog.findViewById(R.id.item_more_tv1);
                    TextView more_tv2 = (TextView) dialog.findViewById(R.id.item_more_tv2);
                    TextView more_tv3 = (TextView) dialog.findViewById(R.id.item_more_tv3);
                    TextView more_tv4 = (TextView) dialog.findViewById(R.id.item_more_tv4);
                    ImageView more_close = (ImageView) dialog.findViewById(R.id.item_close);
                    ImageView bt = (ImageView) v;
                    Integer curPosition = (Integer) bt.getTag();
                    more_tv1.setText(data.get(curPosition).get("data5").toString());
                    more_tv2.setText(data.get(curPosition).get("data6").toString());
                    more_tv3.setText(data.get(curPosition).get("data7").toString());
                    more_tv4.setText(data.get(curPosition).get("data8").toString());
                    more_close.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            popupWindow.dismiss();
                        }
                    });
                    //dialog.setY(v.getY());
                    AnimationSet animationSet = new AnimationSet(true);

                    ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1, 1, 1, Animation.ZORDER_BOTTOM, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                    animationSet.setDuration(250);
                    animationSet.addAnimation(scaleAnimation);
                    dialog.startAnimation(animationSet);
                    //处理最后一个item 浮动窗口 显示不全
                    DisplayMetrics dm = new DisplayMetrics();
                    int[] position = new int[2];
                    dm = context.getResources().getDisplayMetrics();
                    int screenHeight = dm.heightPixels; // 屏幕高（像素，如：800px）
                    getRootivew().getLocationInWindow(position);
                    int rootViewY = position[1] + getRootivew().getHeight() + 10;
                    int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
                    int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
                    dialog.measure(w, h);
                    int popupWindowHeight = dialog.getMeasuredHeight();
                    if (rootViewY + popupWindowHeight < screenHeight) {
                        popupWindow.showAsDropDown(getRootivew());
                    } else {
                        popupWindow.showAsDropDown(getRootivew(), 0, screenHeight - (rootViewY + popupWindowHeight));
                    }
                }
            });
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (data != null) {
//            convertView.setTag(convertView.hashCode(), data.get(position).get("index"));
            if (holder.index != null) {
                holder.index.setText(data.get(position).get("index").toString());
            }
            if (holder.tv5 != null) {
                holder.tv5.setText(data.get(position).get("czzabbre").toString());
            }
            if (holder.tv6 != null) {
                holder.tv6.setText(data.get(position).get("sum").toString());
            }
            if (holder.tv1 != null) {
                holder.tv1.setText(data.get(position).get("cvouchid").toString());
            }
            if (holder.tv2 != null) {
                holder.tv2.setText(data.get(position).get("cdept").toString());
            }
            if (holder.tv3 != null) {
                holder.tv3.setText(data.get(position).get("cnocheckreason").toString());
            }
            if (holder.tv4 != null) {
                holder.tv4.setText(data.get(position).get("ddate").toString());
            }
            if (data.get(position).get("more") != null) {
                holder.more.setVisibility(View.GONE);
            } else {
                holder.more.setVisibility(View.VISIBLE);
            }
            int index = Integer.valueOf(data.get(position).get("index").toString()) - 1;
            if (isSelected.size() > 0 && isSelected.size() > index) {
                if (getIsSelected().get(index)) {
                    //选中
                    holder.background.setImageResource(R.drawable.itembox_choose);
                } else {
                    //未选
                    holder.background.setImageResource(R.drawable.itembox_zi);
                }
            }
        }
        return convertView;
    }

    public HashMap<Integer, Boolean> getIsSelected() {
        return isSelected;
    }

    public void setIsSelected(HashMap<Integer, Boolean> isSelected) {
        this.isSelected = isSelected;
    }

    private class ViewHolder {
        ImageView background;
        TextView index;
        TextView tv1;
        TextView tv2;
        TextView tv3;
        TextView tv4;
        TextView tv5;
        TextView tv6;
        ImageView more;
    }

    private class MyOnClickListener implements View.OnClickListener {
        private View rootView;

        public MyOnClickListener(View view) {
            rootView = view;
        }

        public View getRootivew() {
            return rootView;
        }

        @Override
        public void onClick(View view) {

        }
    }
}